﻿#region Copyright and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ucenter
 * ==============================================================================*/
#endregion
using System;
using System.Linq;
using UCenter.Core.Repository;
using UCenter.DataProvider;
using UCenter.Models;


namespace UCenter.SQLServerProvider
{
    /// <summary>
    /// The repository of account
    /// </summary>
    public class AccountRepository : SqlRepositoryBase<User>, IAccountRepository
    {
        #region IAccountRepository 成员
        /// <summary>
        /// Inserts the user.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public User InsertUser(User user)
        {
            // the methods we used to access the table will be changed. As:
            // InserEntity(user);   
            using (RepositoryContext db = new RepositoryContext())
            {
                db.GetTable<User>().InsertOnSubmit(user);
                db.SubmitChanges();
            }

            return user;
        }

        // TODO: This method will be changed
        public bool ChangePassword(string email, string oldPassword, string newPassword)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                User user = db.GetTable<User>().Single(u => u.Email == email);

                if (user != null)
                {
                    if (user.Password != null && oldPassword == user.Password)
                    {
                        user.Password = newPassword;

                        db.SubmitChanges();

                        return true;
                    }
                }
            }

            return false;
        }

        public void UpdateOnline(int userID, bool online)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                var user = (from u in db.GetTable<User>()
                            where u.ID == userID
                            select u).SingleOrDefault<User>();
                user.IsOnline = online;
                db.SubmitChanges();
            }
        }

        public void SetStatus(int userID, string status)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                var p = db.GetTable<Profile>().First<Profile>(u => u.ID == userID);
                p.State = status;
                p.StateRefreshTime = DateTime.Now;
                db.SubmitChanges();
            }
        }
        public void UpdateUserLogo(int userID, string userLogo)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                var p = db.GetTable<Profile>().First<Profile>(u => u.ID == userID);
                p.Avatar = userLogo;
                db.SubmitChanges();
            }
        }

        public void UpdateProfile(Profile p)
        {
            using (RepositoryContext db = new RepositoryContext())
            {
                var profile = db.GetTable<Profile>().First<Profile>(u => u.ID == p.ID);
                profile.Birthday = p.Birthday;
                profile.Gender = p.Gender;
                profile.Hometown = p.Hometown;
                profile.MSN = p.MSN;
                profile.NickName = p.NickName;
                profile.PhoneNumber = p.PhoneNumber;
                profile.QQ = p.QQ;
                profile.State = p.State;
                profile.TrueName = p.TrueName;
                profile.StateRefreshTime = p.StateRefreshTime;
                profile.Avatar = p.Avatar;
                profile.Website = p.Website;
                db.SubmitChanges();
            }
        }      
        #endregion
    }
}
